"""
作者：Leagolas
日期：2024年06月13日
"""

import VolatilityCalculating
import TradingSignal
import OptionsChange
import pandas as pd
import numpy as np

sse50ETF_data = pd.read_excel('./data/sse50ETF.xlsx')
options_data = pd.read_excel('./data/options.xlsx')

volatility_data = VolatilityCalculating.Parkison_volatility(sse50ETF_data, 5)
trading_signal = TradingSignal.calculate_bollinger_bands(volatility_data, "Parkinson波动率估计量", 5)
# trading_signal.to_excel("trading_signal.xlsx")


recorded_condition = ['nan']
recorded_position_type = ['none']
position_pd = pd.DataFrame(trading_signal['交易日'].copy())
position_pd['position_type'] = pd.NA
position_pd['call_option'] = pd.NA
position_pd['put_option'] = pd.NA
position_pd['cash_flow'] = pd.NA
position_pd['cash'] = 1000000000
position_pd['portfolio'] = 0


recorded_month = [str(trading_signal.iloc[0]['交易日'])[2:4] + str(trading_signal.iloc[0]['交易日'])[5:7]]

for i in range(0,trading_signal.shape[0]):
    print(i)
    current_condition = [trading_signal.iloc[i]["状态"]]
    current_month = [str(trading_signal.iloc[i]['交易日'])[2:4] + str(trading_signal.iloc[i]['交易日'])[5:7]]


    # if current_month[0] != recorded_month[0]:
    #
    #     attribute_list = ['position_type', 'call_option', 'put_option']
    #     position_pd.loc[i, attribute_list] = position_pd.loc[i - 1, attribute_list]
    #
    #     OptionsChange.close_position(i, position_pd, recorded_position_type, options_data)
    #     recorded_month[0] = current_month[0]


    if recorded_position_type[0] == 'none':
        # 当前波动率过高，预计波动率下降，做空波动率
        if current_condition[0] == 2 and recorded_condition[0] <= 2:
            OptionsChange.short_volatility(i ,recorded_position_type, current_month, options_data, sse50ETF_data,position_pd)


        elif current_condition[0] == -2 and recorded_condition[0] >= -2:
            OptionsChange.long_volatility(i, recorded_position_type, current_month, options_data, sse50ETF_data, position_pd)

        else:
            position_pd.loc[i, 'cash_flow'] = 0
            if i > 1:
                position_pd.loc[i, 'portfolio'] = position_pd.loc[i - 1, 'portfolio']
                position_pd.loc[i, 'cash'] = position_pd.loc[i - 1, 'cash']





    elif recorded_position_type[0] == 'long':
        attribute_list = ['position_type', 'call_option', 'put_option']
        position_pd.loc[i, attribute_list] = position_pd.loc[i - 1, attribute_list]

        if current_condition[0] > -1:
            OptionsChange.close_position(i, position_pd, recorded_position_type, options_data)

        else:
            OptionsChange.daily_settlement(i, position_pd, recorded_position_type, options_data)



    elif recorded_position_type[0] == 'short':
        attribute_list = ['position_type', 'call_option', 'put_option']
        position_pd.loc[i, attribute_list] = position_pd.loc[i - 1, attribute_list]
        if current_condition[0] < 1:
            OptionsChange.close_position(i, position_pd, recorded_position_type, options_data)

        else:
            OptionsChange.daily_settlement(i, position_pd, recorded_position_type, options_data)


    recorded_condition[0] = current_condition[0]

position_pd['cash'] = position_pd['cash'] - position_pd['cash'].min()
position_pd['asset'] = position_pd['cash'] + position_pd['portfolio']
position_pd.to_excel("./outcome/pure_delta_position.xlsx")